﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite
{
    public partial class SearchResult : PreBasePage
    {
        private string keyword = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            keyword =Server.UrlDecode(Request.QueryString["keyword"]) + "";
            if (keyword == "")
            {
                keyword = Session["keyword"] + "";//关键字
            }
            

            if (!Page.IsPostBack)
            {
                BindBlog();
                BindCompany();
                BindUser();
            }
        }

        #region 加载博客
        private void BindBlog()
        {
            
            //设置控件属性，从BasePage中得到
            PageConfig(BlogUPager);

            //List<Member> list = new List<Member();
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();
            Criteria c1 = new Criteria();
            c1.Mode = CriteriaMode.Or;

            //加载有效的
            c.Add(CriteriaType.Equals, "Flag", 0);
            //查询条件
            string[] spit = new string[] { " ","+",","};
            string[] arraykeyword = new string[]{};
            bool isspit = false;
            for (int i = 0; i < spit.Length; i++)
            {
                if (keyword.Contains(spit[i]))
                {
                    arraykeyword = keyword.Split(' ');
                    isspit = true;
                }
            }
            if (!isspit)
            {
                c1.AddOr(CriteriaType.Like, "Title", "%" + keyword + "%");
                c1.AddOr(CriteriaType.Like, "NickName", "%" + keyword + "%");
                c1.AddOr(CriteriaType.Like, "KeyWord", "%" + keyword + "%");
                c1.AddOr(CriteriaType.Like, "BlogBigType", "%" + keyword + "%");
                c1.AddOr(CriteriaType.Like, "BlogTypeName", "%" + keyword + "%");
            }
            else
            {
                for (int i = 0; i < arraykeyword.Length; i++)
                {
                    c1.AddOr(CriteriaType.Like, "Title", "%" + arraykeyword[i] + "%");
                    c1.AddOr(CriteriaType.Like, "NickName", "%" + arraykeyword[i] + "%");
                    c1.AddOr(CriteriaType.Like, "KeyWord", "%" + arraykeyword[i] + "%");
                    c1.AddOr(CriteriaType.Like, "BlogBigType", "%" + keyword + "%");
                    c1.AddOr(CriteriaType.Like, "BlogTypeName", "%" + keyword + "%");
                }
            }
            c.Criterias.Add(c1);

            BlogUPager.ItemCount = bhelper.QueryBlogCountBySearch(c);

            // 以下的值从BasePage中得到
            BlogUPager.UrlFormat = UrlFormat;
            //BlogUPager.PrefixText = GetPreFixText(BlogUPager);

            string[] fields = new string[] { "ID", "BlogBigType", "BlogTypeName", "CompanyID", "BlogTypeID", "Title", "BlogContent", "UsersID", "NickName", "PVCount", "ReplyCount", "RecommandCount", "HasPic", "Flag", "CreateDate" };

            DataTable dt = new DataTable();
            dt = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, BlogUPager.Begin - 1, BlogUPager.Count, fields);

            dt.Columns.Add(new DataColumn("strBlogContent"));
            dt.Columns.Add(new DataColumn("strHasPic"));
            dt.Columns.Add(new DataColumn("strVideoPic"));
            dt.Columns.Add(new DataColumn("companyname"));//小组

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    //截取内容
                    string blogcontent = Server.HtmlDecode(dr["BlogContent"] + "");
                    //将内容中的html代码去掉
                    blogcontent = Tools.NoHTML(blogcontent);
                    string strblogcontent = Tools.GetFirstString(blogcontent, 160);
                    if (strblogcontent != "")
                    {
                        string userid = dr["UsersID"] + "";
                        //加载头像
                        UsersHelper uhelper = new UsersHelper();
                        Users ubloginfo = uhelper.GetUsers(userid);
                        string headphoto = ubloginfo.SmallHeadImage;
                        if (!string.IsNullOrEmpty(headphoto))
                        {
                            dr["strBlogContent"] = "<p class=\"post_item_summary\" ><a href=\"u/" + userid + ".aspx\" target=\"_blank\"><img align=\"left\" width=\"48\" height=\"48\"  class=\"pfs\" src=\"" + headphoto + "\" alt=\"\"/></a>" + strblogcontent + "</p>";
                        }
                        else
                        {
                            dr["strBlogContent"] = "<p class=\"post_item_summary\" ><a href=\"u/" + userid + ".aspx\" target=\"_blank\"><img align=\"left\" width=\"48\" height=\"48\"  class=\"pfs\" src=\"images/morentouxiang.jpg\" alt=\"\"/></a>" + strblogcontent + "</p>";
                        }
                    }
                    //判断是否存在图片
                    string haspic = dr["haspic"] + "";
                    if (haspic == "1")
                    {
                        dr["strHasPic"] = "<img src=\"images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                    }
                    else
                    {
                        dr["strHasPic"] = "";
                    }

                    //string blogbigtype = dr["BlogBigType"] + "";
                    //if (blogbigtype == "视频")
                    //{
                    //    dr["strVideoPic"] = "<img src=\"images/smallvideo.jpg\" alt=\"视频\"/>";
                    //}
                    //else if (blogbigtype.ToLower() == "mp3")
                    //{
                    //    dr["strVideoPic"] = "<img src=\"images/mp3.png\" alt=\"mp3\"/>";
                    //}
                    //else
                    //{
                    //    dr["strVideoPic"] = "";
                    //}

                    string blogbigtype = dr["BlogBigType"] + "";
                    if (blogbigtype == "视频")
                    {
                        dr["strVideoPic"] = "<img src=\"images/smallvideo.jpg\" alt=\"视频\"/>";
                        dr["BlogBigType"] = "<a href=\"VideoList.aspx\" target=\"_blank\"><font color=\"#729830\">" + blogbigtype + "</font></a>";
                    }
                    else if (blogbigtype.ToLower() == "mp3")
                    {
                        dr["strVideoPic"] = "<img src=\"images/mp3.png\" alt=\"mp3\"/>";
                        dr["BlogBigType"] = "<a href=\"MP3List.aspx\" target=\"_blank\"><font color=\"#729830\">" + blogbigtype + "</font></a>";
                    }
                    else
                    {
                        dr["strVideoPic"] = "";
                        dr["BlogBigType"] = "<a href=\"TextList.aspx\" target=\"_blank\"><font color=\"#729830\">" + blogbigtype + "</font></a>";
                    }

                    string blogtypename = dr["BlogTypeName"] + "";
                    string blogtypeid = dr["BlogTypeID"] + "";
                    dr["BlogTypeName"] = "<a href=\"cate/" + blogtypeid + ".aspx\" target=\"_blank\"><font color=\"#729830\">" + blogtypename + "</font></a>";
                    //加载小组
                    string companyid = dr["CompanyID"] + "";
                    if (!string.IsNullOrEmpty(companyid) && companyid != "0")
                    {
                        Companys cinfo = new Companys();
                        CompanysHelper chelper = new CompanysHelper();
                        cinfo = chelper.GetCompanys(companyid);
                        dr["companyname"] = "小组：" + cinfo.CompanyName;
                    }
                    else
                    {
                        dr["companyname"] = "";
                    }
                }

                rptBlog.DataSource = dt;
                rptBlog.DataBind();
                lblMessage.Visible = false;
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = "没有查询到符合条件的结果，<a href=\"Index.aspx\"><font color=\"red\">返回首页</font></a>";
            }

            
        }
        #endregion

        #region 加载小组
        private void BindCompany()
        {
            CompanysHelper chelper = new CompanysHelper();

            Criteria c = new Criteria();
            Criteria c1 = new Criteria();
            c1.Mode = CriteriaMode.Or;

            //加载有效的
            c.Add(CriteriaType.Equals, "Flag", 0);
            //查询条件
            string[] spit = new string[] { " ", "+", "," };
            string[] arraykeyword = new string[] { };
            bool isspit = false;
            for (int i = 0; i < spit.Length; i++)
            {
                if (keyword.Contains(spit[i]))
                {
                    arraykeyword = keyword.Split(' ');
                    isspit = true;
                }
            }
            if (!isspit)
            {
                c1.AddOr(CriteriaType.Like, "CompanyName", "%" + keyword + "%");
                c1.AddOr(CriteriaType.Like, "Brief", "%" + keyword + "%");
                c1.AddOr(CriteriaType.Like, "OwnerNickName", "%" + keyword + "%");
            }
            else
            {
                for (int i = 0; i < arraykeyword.Length; i++)
                {
                    c1.AddOr(CriteriaType.Like, "CompanyName", "%" + arraykeyword[i] + "%");
                    c1.AddOr(CriteriaType.Like, "Brief", "%" + arraykeyword[i] + "%");
                    c1.AddOr(CriteriaType.Like, "OwnerNickName", "%" + arraykeyword[i] + "%");
                }
            }
            c.Criterias.Add(c1);

            string[] fields = new string[] { "ID", "CompanyName", "Brief", "BlogPVNum", "Flag" };
            DataTable dt = chelper.QueryCompanysBySearchToTable(c, new Order[] { new Order("BlogPVNum", OrderMode.Desc) }, 0, 40, fields);

            dt.Columns.Add(new DataColumn("strCompanyName"));
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string companyname = dr["CompanyName"] + "";
                    string strcompanyname = Tools.GetFirstString(companyname, 20);
                    dr["strCompanyName"] = strcompanyname;
                }

                rptCompany.DataSource = dt;
                rptCompany.DataBind();
                lblCompany.Visible = false;
            }
            else
            {
                lblCompany.Visible = true;
                lblCompany.Text = "没有查到符合条件小组";
            }

            
        }
        #endregion

        #region 加载人才
        private void BindUser()
        {
            UsersHelper uhelper = new UsersHelper();

            Criteria c = new Criteria();
            Criteria c1 = new Criteria();
            c1.Mode = CriteriaMode.Or;

            //加载有效的
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "RoleType", 1);
            //查询条件
            string[] spit = new string[] { " ", "+", "," };
            string[] arraykeyword = new string[] { };
            bool isspit = false;
            for (int i = 0; i < spit.Length; i++)
            {
                if (keyword.Contains(spit[i]))
                {
                    arraykeyword = keyword.Split(' ');
                    isspit = true;
                }
            }
            if (!isspit)
            {
                c1.AddOr(CriteriaType.Like, "UserName", "%" + keyword + "%");
                c1.AddOr(CriteriaType.Like, "NickName", "%" + keyword + "%");
                c1.AddOr(CriteriaType.Like, "Brief", "%" + keyword + "%");
            }
            else
            {
                for (int i = 0; i < arraykeyword.Length; i++)
                {
                    c1.AddOr(CriteriaType.Like, "UserName", "%" + arraykeyword[i] + "%");
                    c1.AddOr(CriteriaType.Like, "NickName", "%" + arraykeyword[i] + "%");
                    c1.AddOr(CriteriaType.Like, "Brief", "%" + arraykeyword[i] + "%");
                }
            }
            c.Criterias.Add(c1);

            string[] fields = new string[] { "ID", "Brief", "BlogPVNum", "NickName" };

            DataTable dtUser = uhelper.QueryUsersBySearchToTable(c, new Order[] { new Order("BlogPVNum", OrderMode.Desc) }, 0, 20, fields);

            if (dtUser != null && dtUser.Rows.Count > 0)
            {
                rptUser.DataSource = dtUser;
                rptUser.DataBind();
                lblUser.Visible = false;
            }
            else
            {
                lblUser.Visible = true;
                lblUser.Text = "没有查到符合条件用户";
            }

        }
        #endregion
    }
}